package com.nannybear.Controller;


import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nannybear.R;

/**
 * \brief Controller of the remote Screen
 * \author Audrey LOISEAU
 * \date 11-05-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class RemoteController {
    /********** DECLARATIONS **********/
    // Class
    private ViewPuppeteer viewPuppeteer;

    //Variables
    private boolean animation1Open;
    private boolean animation2Open;

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param viewPuppeteer The viewPuppeteer class
     */
    public RemoteController(ViewPuppeteer viewPuppeteer){
        this.viewPuppeteer = viewPuppeteer;
        animation1Open = false;
        animation2Open = false;
    }

    /**
     * \brief Initialization of the view (creation of widget and listener)
     */
    public void initInterface(){
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLayout_led((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.layout_led));
        this.viewPuppeteer.getmActivity().getRemoteScreen().setAnimation_layout((RelativeLayout) this.viewPuppeteer.getmActivity().findViewById(R.id.animation_layout));
        this.viewPuppeteer.getmActivity().getRemoteScreen().setTime((TextView) this.viewPuppeteer.getmActivity().findViewById(R.id.time_led));
        this.viewPuppeteer.getmActivity().getRemoteScreen().setGifView((WebView) this.viewPuppeteer.getmActivity().findViewById(R.id.gifView));
        final ImageButton animation_btn = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.animation_btn);
        final ImageButton start_btn = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.start_btn);
        final ImageButton led_btn = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.light_btn);
        final ImageView led_part1_1 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part1_1);
        final ImageView led_part1_2 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part1_2);
        final ImageView led_part2_1 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part2_1);
        final ImageView led_part2_2 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part2_2);
        final ImageView led_part3_1 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part3_1);
        final ImageView led_part3_2 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part3_2);
        final ImageView led_part4_1 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part4_1);
        final ImageView led_part4_2 = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_part4_2);
        final ImageView led_middle = (ImageView) this.viewPuppeteer.getmActivity().findViewById(R.id.led_middle);

        final Button start = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.start);
        final Button stop = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.stop);
        final Button animation_1 = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.animation_1);
        final Button animation_2 = (Button) this.viewPuppeteer.getmActivity().findViewById(R.id.animation_2);
        final ImageButton time_btn = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.button_time);
        final ImageButton lullaby = (ImageButton) this.viewPuppeteer.getmActivity().findViewById(R.id.start_lullaby);

        // Listener
        animation_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeNightLight(animation_btn);
                viewPuppeteer.getmActivity().getRemoteScreen().getGifView().setVisibility(View.GONE);
            }
        });
        start_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeNightLight(start_btn);
            }
        });
        led_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeNightLight(led_btn);
            }
        });
        led_part1_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(1);
            }
        });
        led_part1_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(1);
            }
        });
        led_part2_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(2);
            }
        });
        led_part2_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(2);
            }
        });
        led_part3_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(3);
            }
        });
        led_part3_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(3);
            }
        });
        led_part4_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("RemotC","click 4_2");
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(4);
            }
        });
        led_part4_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("RemotC","click 4_1");
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(4);
            }
        });
        led_middle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPuppeteer.getmActivity().getRemoteScreen().changeLedBoard(5);
            }
        });
        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getConnection().isConnected()) {
                    sendControllerNightLight();
                }
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getConnection().isConnected()) {
                    viewPuppeteer.getmActivity().getConnection().getProxyRobot().sendStateNightLight(0);
                }
            }
        });
        time_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int marginTop = 0, marginStart = 0;
                ViewGroup.MarginLayoutParams timeMargin = (ViewGroup.MarginLayoutParams) viewPuppeteer.getmActivity().getRemoteScreen().getTime().getLayoutParams();
                String time = viewPuppeteer.getmActivity().getRemoteScreen().getTime().getText().toString();
                if(time.equals("15")){
                    time = "30";
                    marginTop = 90;
                    marginStart = 426;
                }
                else if(time.equals("30")){
                    time = "60";
                    marginTop = 90;
                    marginStart = 426;
                }
                else if(time.equals("60")){
                    time = "120";
                    marginTop = 90;
                    marginStart = 422;
                }
                else if(time.equals("120")){
                    time = "CONTINUOUS";
                    marginTop = 165;
                    marginStart = 360;
                }
                else if(time.equals("CONTINUOUS")){
                    time = "15";
                    marginTop = 90;
                    marginStart = 426;
                }
                viewPuppeteer.getmActivity().getRemoteScreen().getTime().setText(String.valueOf(time));
                timeMargin.setMargins(marginStart, marginTop, 0, 0);
            }
        });
        animation_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation_1.setBackgroundResource(R.drawable.button_drawable);
                animation_2.setBackgroundResource(R.drawable.button_grey_drawable);
                animation1Open = true;
                animation2Open = false;
                viewPuppeteer.getmActivity().getRemoteScreen().loadGif(1);
            }
        });
        animation_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animation_2.setBackgroundResource(R.drawable.button_drawable);
                animation_1.setBackgroundResource(R.drawable.button_grey_drawable);
                animation1Open = false;
                animation2Open = true;
                viewPuppeteer.getmActivity().getRemoteScreen().loadGif(2);
            }
        });
        lullaby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(viewPuppeteer.getmActivity().getConnection().isConnected()){
                    viewPuppeteer.getmActivity().getConnection().getProxyRobot().startLullaby();
                }
            }
        });

        // Instantiation
        this.viewPuppeteer.getmActivity().getRemoteScreen().setAnimation_btn(animation_btn);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setDefault_btn(start_btn);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_btn(led_btn);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part1_1(led_part1_1);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part1_2(led_part1_2);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part3_1(led_part3_1);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part3_2(led_part3_2);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part2_1(led_part2_1);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part2_2(led_part2_2);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part4_1(led_part4_1);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_part4_2(led_part4_2);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setLed_middle(led_middle);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setStart(start);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setStop(stop);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setAnimation_1(animation_1);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setAnimation_2(animation_2);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setTime_btn(time_btn);
        this.viewPuppeteer.getmActivity().getRemoteScreen().setStart_lullaby(lullaby);
    }

    /**
     * \brief Send the good value of the night light to the robot
     */
   private void sendControllerNightLight(){
       if(viewPuppeteer.getmActivity().getRemoteScreen().isDefaultIsOpen()){
           viewPuppeteer.getmActivity().getConnection().getProxyRobot().sendStateNightLight(viewPuppeteer.getmActivity().getRemoteScreen().getColorLed());
       }
       else if(viewPuppeteer.getmActivity().getRemoteScreen().isLedIsOpen()){
           int time;
           if(viewPuppeteer.getmActivity().getRemoteScreen().getTime().getText().toString().equals("CONTINUOUS")){
               time = 0;
           }
           else{
               time = Integer.parseInt(viewPuppeteer.getmActivity().getRemoteScreen().getTime().getText().toString());
           }
           viewPuppeteer.getmActivity().getConnection().getProxyRobot().sendSelectedLed(
                   String.valueOf(viewPuppeteer.getmActivity().getRemoteScreen().getColorLedPart1())
                           + String.valueOf(viewPuppeteer.getmActivity().getRemoteScreen().getColorLedPart2())
                           + String.valueOf(viewPuppeteer.getmActivity().getRemoteScreen().getColorLedPart3())
                           + String.valueOf(viewPuppeteer.getmActivity().getRemoteScreen().getColorLedPart4())
                           + String.valueOf(viewPuppeteer.getmActivity().getRemoteScreen().getColorLedMiddle())
                           + time / 15);
       }
       else if(viewPuppeteer.getmActivity().getRemoteScreen().isAnimationIsOpen()){
           if(animation1Open){
               viewPuppeteer.getmActivity().getConnection().getProxyRobot().sendAnimation(1);
           }
           else if(animation2Open){
               viewPuppeteer.getmActivity().getConnection().getProxyRobot().sendAnimation(2);
           }
       }
   }
}
